<?php
uses('model');

class forumModel extends Model {

    function __construct() {
        parent::__construct();
    }

    function topicList($v) {
        $this->dbconnect();

        if ($data = $this->db->GetRow($this->table['forum'], 'subject,body', "id={$v['id']} AND fid=0")) {
            if ($v['pagesize']) {
                $data['list'] = $this->db->GetPage(array($this->table['forum'], '*', "fid={$v['id']} ORDER BY ".$v['sort']." {$v['sortorder']}"), $data['page'], $v['pagesize']);
            } else {
                $data['list'] = $this->db->GetAll($this->table['forum'], '*', "fid={$v['id']} ORDER BY ".$v['sort']." {$v['sortorder']}");
            }
        } else {
            $data['code']['topicList'] = 4;
        }

        return $data;
    }

    function newPost($v) {
        $this->dbconnect();

        $fields['fid']      = $v['id'];
        $fields['subject']  = $v['subject'];
        $fields['body']     = $v['body'];
        $fields['uid']      = $v['uid'];
        $fields['uname']    = $v['uname'];
        $fields['umail']    = $v['umail'];
        $fields['created']  = time();
        $fields['updated']  = time();

        //检查要发布到的版块是否存在
        if ($this->db->Count($this->table['forum'], "id={$v['id']} AND fid=0"))
            $data['code']['newPost'] = $this->db->Insert($this->table['forum'], $fields) ? 1 : 2;
        else
            $data['code']['newPost'] = 64;

        return $data;
    }

    function delPost($v) {
        $this->dbconnect();

        return $data['code']['delReply'] = ($this->db->Delete($this->table['forum'], "id={$v['remove']} AND fid<>0") && $this->db->Delete($this->table['topic'], "fid={$v['remove']}")) ? 1 : 2;
    }

    function newReply($v) {
        $this->dbconnect();

        $fields['fid']      = $v['tid'];
        $fields['body']     = $v['body'];
        $fields['uid']      = $v['uid'];
        $fields['uname']    = $v['uname'];
        $fields['umail']    = $v['umail'];
        $fields['isadmin']  = $v['isadmin'];
        $fields['updated']  = time();

        //检查要回复的主题是否存在
        if ($row = $this->db->GetRow($this->table['forum'], 'id,fid,uname,umail', "id={$v['tid']} AND fid<>0")) {
            //防止重复回复
            if ($this->db->Count($this->table['topic'], "fid={$v['tid']} AND uname=".$this->db->qstr($fields['uname'])." AND body=".$this->db->qstr($fields['body'])) == 0) {
                $data['code']['newReply'] = $this->db->Insert($this->table['topic'], $fields) ? 1 : 2;

                $this->_recountTopic($v['tid']);
                //$this->db->Update($this->table['forum'], array('tcount'=>'{[+1]}', 'updated'=>time()), "id={$v['tid']}");

                if ($row['umail'] && ($row['umail'] != $fields['umail'])) {
                    //Mail通知作者
                    $f['sendto']   = $row['uname'];
                    $f['mailto']   = $row['umail'];
                    $f['subject']  = '您在乐铺的话题有人回复';
                    $f['body']     = "亲爱的{$row['uname']}：\r\n\r\n".
                    $f['body']    .= "您在乐铺发表的话题有人回复了，请点击 http://www.loopo.cn/forum/{$row['fid']}/{$row['id']} 查看。\r\n\r\n";
                    $f['body']    .= "有任何问题请及时联系我们。";
                    $f['created']  = time();

                    $this->db->Insert($this->table['mail'], $f);
                }

            } else {
                $data['code']['newReply'] = 4;
            }
        } else {
                $data['code']['newReply'] = 64;
        }

        return $data;
    }

    function delReply($v) {
        $this->dbconnect();

        $result = $data['code']['delReply'] = $this->db->Delete($this->table['topic'], "id={$v['remove']}") ? 1 : 2;

        $this->_recountTopic($v['tid']);

        //回复数 -1
        //if ($result==1) $this->db->Update($this->table['forum'], array('tcount'=>'{[-1]}', 'updated'=>time()), "id={$v['tid']}");

        return $result;
    }

    function getTopic($v) {
        $this->dbconnect();

        if ($data['top'] = $this->db->GetRow($this->table['forum'], '*', "id={$v['tid']}")) {
            $data['list'] = $this->db->GetAll($this->table['topic'], '*', "fid={$v['tid']} ORDER BY id ASC");
        } else {
            $data['code']['getTopic'] = 4;
        }

        return $data;
    }

    function _recountTopic($tid) {
        $this->dbconnect();

        $count = $this->db->Count($this->table['topic'], "fid=$tid");
        return $this->db->Update($this->table['forum'], array('tcount'=>$count, 'updated'=>time()), "id=$tid");
    }
}
?>
